<!-- 物料列表信息 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form toolbar">
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-input-inline mr0" style="width: 240px">
              <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编码" />
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-input-inline mr0" style="width: 240px">
              <input name="skuSpec" class="layui-input" type="text" placeholder="请输入物料规格" />
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-input-inline">
              <input name="productTypeId" type="hidden" />
              <div class="layui-input-inline mr0" id="selectProductType"></div>
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn icon-btn" lay-filter="productDetailFormSubSearchLog" lay-submit style="padding: 0 24px">搜索</button>
            <button id="productDetailConfirm" class="layui-btn">确认选择</button>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <div class="scroll-table-4-5">
        <table class="layui-table" id="productDetailTable" lay-filter="productDetailTable"></table>
      </div>
    </div>
  </div>
</div>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "table", "admin"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;

    //物料类别
    admin.initDataSelectTree(true, "enterprise/product/admin/productType/getTreeData", null, "selectProductType", null, "物料类别", "productTypeId", null);
    form.render("select");
    const nowData = { ...parentData };
    //订单编号
    let orderId = nowData.orderId;
    let type = nowData.type;
    let url = "";
    let where = {};
    if (orderId && type) {
      url = config.base + "enterprise/product/admin/product/getQuantityBy";
      where = { orderId: orderId, type: type };
    } else {
      url = config.base + "enterprise/product/admin/productDetail/list";
      where = {};
    }
    // 渲染表格
    var productDetailTable = table.render({
      elem: "#productDetailTable",
      url: url,
      where: where,
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          { type: "radio" },
          { field: "skuCode", title: "物料编码", align: "center" },
          { field: "skuName", title: "物料名称", align: "center" },
          { field: "skuSpec", title: "物料规格", align: "center" },
          { field: "productTypeName", title: "物料类别", align:  "center" },
          { field: "baseUnitName", title: "基础单位", align: "center" },
          { field: "mainUnitName", title: "主计量单位", align: "center" },
          { field: "haveQuantity", title: "库存数量", align: "center" },
          { field: "createTime", title: "创建时间", align: "center" }
        ]
      ]
    });
    // 搜索
    form.on("submit(productDetailFormSubSearchLog)", function (data) {
      productDetailTable.reload(
        {
          where: data.field,
          page: { curr: 1 }
        },
        "data"
      );
    });

    // 确认选择
    $("#productDetailConfirm").click(async function () {
      let checkStatus = table.checkStatus("productDetailTable"); //test即为基础参数id对应的值
      let dataS = checkStatus.data;
      if (checkStatus.data.length == 0) {
        layer.msg("请选择物料", { icon: 2, time: 1000 });
      } else {
        let productDetail = dataS[0];
        console.log("productDetail", productDetail);

        form.val("addProductionPlanForm", {
          customerSkuCode: productDetail.skuCode,
          customerSkuName: productDetail.skuName,
          customerSkuSpec: productDetail.skuSpec,
          residueQuantity: productDetail.residueQuantity,
          demandQuantity: productDetail.quantity,
          productTypeId: productDetail.productTypeId,
          productDetailId: productDetail.productDetailId ? productDetail.productDetailId : productDetail.id,
          productTypeName: productDetail.productTypeName,
          productNatureName: productDetail.productNatureName,
          orderProId: productDetail.orderProId
          // residueQuantity: productDetail.noQuantityNum
        });
        form.val("addProductionPlanFormDiv", {
          customerSkuCode: productDetail.skuCode,
          customerSkuName: productDetail.skuName,
          customerSkuSpec: productDetail.skuSpec,
          residueQuantity: productDetail.residueQuantity,
          demandQuantity: productDetail.quantity,
          productTypeId: productDetail.productTypeId,
          productDetailId: productDetail.productDetailId ? productDetail.productDetailId : productDetail.id,
          productTypeName: productDetail.productTypeName,
          productNatureName: productDetail.productNatureName,
          orderProId: productDetail.orderProId,
          materialNatureName: productDetail.materialNatureName
          // residueQuantity: productDetail.noQuantityNum
        });
        //物料工序列表
        // $("#processId").empty();
        // admin.initDataSelect('enterprise/echnologyParam/admin/echnologyParam/list', { productDetailId: productDetail.id }, 'processId', null, '工序', null, null);

        // const resData = await admin.initDataSelectPro(
        //   "enterprise/benchmarkBook/admin/benchmarkBook/getBookInfoBy/" + productDetail.id,
        //   {},
        //   "bookId",
        //   "基准书",
        //   async (d,obj) => {
        //     form.val("addProductionPlanFormDiv", {
        //       accessoryFileId: ""
        //     });
        //     $("#processId").empty();
        //     $("#accessoryPath").html("<span style='cursor: default;'>暂无附件</span>");
        //     if (d.value) {
        //       await admin.initDataSelectPro("enterprise/benchmarkBook/admin/benchmarkBook/getEchnologyBy/" + d.value, {}, "processId", "工序", null);
        //       if (obj.drawUrl) {
        //         form.val("addProductionPlanFormDiv", {
        //           accessoryFileId: obj.drawUrl
        //         });
        //         $("#accessoryPath").html("<a href='" + obj.drawUrl + "' target='_blank'>" + obj.drawUrl + "</a>");
        //         // $("#accessoryBtn").hide();
        //       }
        //     }
        //   form.render();

        //   },
        //   "code"
        // );
        // 如果没有基准书，跳到基准书页面
        // if (resData && resData.length == 0) {
        //   //弹窗是否跳转
        //   layer.confirm("当前物料没有基准书，是否跳转新增基准书弹窗？", { icon: 3, title: "提示" }, function (index) {
        //     // 关闭所有弹窗
        //     layer.closeAll();
        //     // window.location.href = '/admin/index.html#/res/planning/proTask';
        //     admin.formOpen({
        //       area: ["80%", "88%"],
        //       title: "生成基准书",
        //       path: "components/res/benchmarkBook/benchmarkBookForm.html",
        //       success: function () {
        //         nowData.eventName = "add";
        //         productDetail.productDetailId = productDetail.id;
        //         delete productDetail.id;
        //         nowData.data = productDetail;
        //         // 关闭最近弹窗
        //       }
        //     });
        //   });
        // } else {
        //   layer.close(nowData.zindex);
        // }
        console.log(2222);

        layer.close(nowData.zindex);
      }
    });
  });
</script>
